package com.sky.mapper;


import com.github.pagehelper.Page;
import com.sky.dto.GoodsSalesDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.entity.Orders;
import com.sky.vo.OrderVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;


@Mapper
public interface OrderMapper {

    /**
     * 插入订单数据，需要主键返回。
     *
     * @param order
     *
     **/
    void insert(Orders order);

    /**
     * 根据订单号查询历史订单
     *
     * @param orderNumber
     *
     **/
    @Select("select * from orders where number = #{orderNumber} and user_id = #{userId}")
    Orders getByNumberAndUserId(String orderNumber, Long userId);

    /**
     * 修改订单信息
     * @param orders
     */
    void update(Orders orders);


    /**
     * 用于替换微信支付更新数据库状态
     *
     * @param orderStatus
     * @param orderPaidStatus
     * @param checkOutTime
     * @param orderNumber
     *
     **/
    @Update("update orders set status = #{orderStatus},pay_status = #{orderPaidStatus},checkout_time = #{checkOutTime} " +
            "where number = #{orderNumber}")
    void updateStatus(Integer orderStatus, Integer orderPaidStatus, LocalDateTime checkOutTime, String orderNumber);

    /**
     * 根据条件分页查询订单信息
     *
     * @param ordersPageQueryDTO
     *
     **/
    Page<Orders> pageQuery(OrdersPageQueryDTO ordersPageQueryDTO);

    /**
     * 根据订单id查询订单信息
     *
     * @param id
     *
     **/
    @Select("select * from orders where id = #{id}")
    Orders getById(Long id);

    /**
     * 根据订单id修改状态
     *
     * @param id
     *
     **/
    @Update("update orders set status = #{status} where id = #{id}")
    void updateStatusById(Integer id,Integer status);

    /**
     * 统计该状态的订单数量
     * @param status
     **/
    @Select("select count(*) from orders where status = #{status}")
    Integer countStatus(Integer status);

    @Select("select * from orders where status = #{status} and order_time < #{time}")
    List<Orders> getByStatusAndOrdertimeLT(Integer status, LocalDateTime time);

    @Select("select sum(amount) from orders where order_time between #{beginTime} and #{endTime} and status = 5")
    Double countTurnover(LocalDateTime beginTime, LocalDateTime endTime);

    @Select("select count(*) from orders where order_time between #{beginTime} and #{endTime}")
    Integer countNewUserSum(LocalDateTime beginTime, LocalDateTime endTime);

    @Select("select count(*) from orders where order_time < #{endTime}")
    Integer countUserSum(LocalDateTime endTime);

    /**
     * 根据订单状态统计每天的订单总数
     * @param beginTime
     * @param endTime
     **/

    Integer countOrders(LocalDateTime beginTime, LocalDateTime endTime, Integer status);

    /**
     * 查询商品销量排名
     * @param beginTime
     * @param endTime
     */
    List<GoodsSalesDTO> getSalesTop10(LocalDateTime beginTime, LocalDateTime endTime);

    /**
     *根据动态条件统计订单数量
     * @param map
     */
    Integer countByMap(Map map);

    /**
     * 根据动态条件统计营业额
     * @param map
     */
    Double sumByMap(Map map);

}
